package com.example.safeassistan.hiddendanger.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.safeassistan.R;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;


public class HiddenDangerImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
   // private final int mCountLimit = 3;
    private ArrayList<String> data;
    private LayoutInflater minflater;
    private Context mContext;
    private OnItemClickListener listener;

    private static final int ITEM_ONE=1;
    private static final int ITEM_TWO=2;

    public HiddenDangerImageAdapter(ArrayList<String> data, Context mContext) {
        this.data = data;
        this.minflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    private OnItemClickListener clickListener;

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);
        void onSelected(View view, int position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder;
        if(viewType==ITEM_TWO){
            view = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_addimg,parent,false);//添加
            holder = new AnoViewHolder(view);
        }else {
            view = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_picture_feedback,parent,false);
            holder = new ViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof AnoViewHolder) {

        }else {
            Glide.with(mContext).load(data.get(position)).
                    placeholder(R.mipmap.default_image)
                    .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(10)))
                    .into(((ViewHolder) holder).imageView);
            ((ViewHolder) holder).ivClear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(clickListener!=null){
                        clickListener.onSelected(view, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        // 满 3张图就不让其添加新图
//        if (data != null && data.size() >= mCountLimit) {
//            return mCountLimit;
//        } else {
            return data == null ? 1 : data.size() + 1;
       // }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imageView;
        private ImageView ivClear;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.ivBgImage);
            imageView.setOnClickListener(this);
            ivClear=itemView.findViewById(R.id.ivClear);
            ivClear.setOnClickListener(this::onClick);
        }

        @Override
        public void onClick(View view) {
            if(clickListener!=null){
                clickListener.onSelected(itemView, getAdapterPosition());
            }
        }
    }

    public class AnoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView img;


        public AnoViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.ivBgImage);
            img.setOnClickListener((View.OnClickListener) this);

        }

        @Override
        public void onClick(View view) {
            if(clickListener!=null){
                clickListener.onClick(itemView, getAdapterPosition());
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position>data.size()-1){
            return ITEM_TWO;
        }else {
            return ITEM_ONE;
        }
    }


}
